const model = require('../models/reportModel'),
  errCode = require('../../conf/errorCode'),
  wrap = require('../helper/wrap'),
  moment = require('moment'),
  conf = require('../../conf/const'),
  util = require('../helper/util'),
  _ = require('lodash'),
  {parseIntPageSize} = require('./commonService');

exports.getReportByPage = function(page, size, callback) {
  let errMsgs = null;
  [errMsgs, page, size] = parseIntPageSize(page, size);

  if (errMsgs == null)
    model.getReportByPage(page, size, (err, _rel) => {
      _rel.list.map(item => {
        item.reportTime = util.formatTime(item.reportTime);
        item.channelName = item.channel == 0?"众车论":"精品文章";
      });

      if (err) {
        return callback(err);
      } else {
        return callback(null, _rel);
      }
    });
  else callback({errNo: errCode.INVALID_PARAM_ERROR, extraMsgs: errMsgs});
};

exports.deleteReportByIds = function(ids, callback) {
  if (_.isString(ids)) {
    ids = ids.trim().split(',');
    if (
      !_.every(ids, function(value) {
        return util.isIntegerLike(value);
      })
    ) {
      return callback({errNo: errCode.INVALID_FORMAT_ERROR, extraMsgs: 'ids'});
    }
  } else if (_.isNumber(ids)) {
    ids = [ids];
  } else if (
    !_.isArray(ids) ||
    !_.every(ids, function(value) {
      return util.isIntegerLike(value);
    })
  ) {
    return callback({errNo: errCode.INVALID_FORMAT_ERROR});
  }
  if (ids.length == 0) {
    return callback(null, {deleted: 0});
  }

  model.deleteReportByIds(ids, (err, result) => {
    if (err) return callback(err);
    return callback(null, {
      deleted: result.changedRows,
    });
  });
};
